package com.essence.grabiec104.grab.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.essence.dependencies.mybatis.FactoryBeanBuilder;
import com.essence.dependencies.mybatis.plugins.elapsed.ElapsedInterceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.h2.tools.Server;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.sql.SQLException;

@Configuration
@MapperScan(basePackages = "com.essence.grabiec104.grab.dao", sqlSessionTemplateRef = "sqlSessionTemplate")
public class GrabDSConfig {
    @Value("${spring.datasource.mapper-locations}")
    private String mapperLocations;

    @Bean(name = "sqlSessionFactory")
    @Primary
    public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource dataSource, ApplicationContext applicationContext) throws Exception {
        FactoryBeanBuilder builder = FactoryBeanBuilder.getInstance(applicationContext);
        SqlSessionFactoryBean bean = builder.setDataSource(dataSource).getBean(mapperLocations);
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.addInterceptor(new ElapsedInterceptor());
        bean.setConfiguration(configuration);
        return bean.getObject();
    }

    @Bean(name = "dataSource")
    @Primary
    @DependsOn("h2Server")
    public DataSource dataSource(@Value("${spring.datasource.enable}") boolean enable,
                                 @Value("${spring.datasource.url}") String url,
                                 @Value("${spring.datasource.username}") String username,
                                 @Value("${spring.datasource.password}") String password,
                                 @Value("${spring.datasource.driver-class-name}") String driverClassName,
                                 @Value("${spring.datasource.validation-query}") String validationQuery) {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setDriverClassName(driverClassName);
        dataSource.setEnable(enable);
        dataSource.setValidationQuery(validationQuery);
        return dataSource;
    }

    @Bean(name = "sqlSessionTemplate")
    @Primary
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    @Bean(name = "transactionManager")
    @Primary
    public PlatformTransactionManager transactionManager(@Qualifier("dataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(initMethod = "start", destroyMethod = "stop")
    public Server h2Server() throws SQLException {
        return Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort", "9092");
    }
}
